package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.IAgencyInfoDao;
import com.boss.salary.dao.ISalaryBasedataDao;
import com.boss.salary.dao.ISalaryMonthEndDao;
import com.boss.salary.entity.SalaryBasedata;
import com.boss.salary.service.IFormulaManageService;
import com.boss.salary.service.ISalaryBasedataService;
import com.boss.salary.vo.SalaryMonthEndVO;
import com.boss.sys.utils.SysUtils;
import com.singularsys.jep.JepException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SalaryBasedataServiceImpl implements ISalaryBasedataService {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalaryBasedataServiceImpl.class);

    @Autowired
    private IFormulaManageService formulaManageService;

    @Autowired
    private ISalaryBasedataDao dao;
    @Autowired
    private ISalaryMonthEndDao salaryMonthEndDao;
    @Autowired
    private IAgencyInfoDao agencyInfoDao;

    @Override
    public PageDTO<SalaryBasedata> querySalaryBasedataPage(PaginationDTO paginationDTO,String province) throws AppRuntimeException {
        List<SalaryBasedata> list = dao.querySalaryBasedataPage(paginationDTO,province);
        int total = dao.querySalaryBasedataTotal(province);
        PageDTO<SalaryBasedata> pageDTO = new PageDTO<>();
        pageDTO.setRows(list);
        pageDTO.setTotal(total);
        return pageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSalaryBasedata(SalaryBasedata salaryBasedata,String province) {
       int i= dao.getMaxCode(province);
       if(i==99){
           throw new AppRuntimeException("已存在工资项过多！");
       }
        salaryBasedata.setId(SysUtils.generateGuid());
        salaryBasedata.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        salaryBasedata.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        salaryBasedata.setIsDeleted(0);
        salaryBasedata.setSalaryCode("s"+i++);
        dao.saveSalaryBasedata(salaryBasedata);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSalaryBasedata(SalaryBasedata salaryBasedata) {
        salaryBasedata.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        if(salaryBasedata.getBaseState().equals(SystemDictConstant.DISABLE)){
            salaryMonthEndDao.updateRealAndWages(salaryBasedata,null);
            salaryMonthEndDao.clearSalaryMonthEndByColum(salaryBasedata.getSalaryCode(),null);
        }
        dao.updateSalaryBasedata(salaryBasedata);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitSalaryBasedata(List<SalaryBasedata> salaryBasedataList,String province) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, JepException {
        List<String> agencyIds = agencyInfoDao.selectAllAgencyIdByProvince(province);
        List<SalaryMonthEndVO> salaryMonthEndVOList = salaryMonthEndDao.selectSalaryMonthEndWithSalaryType(province, agencyIds);
        Class<?> salaryMonthEndVOClass = SalaryMonthEndVO.class;
        String[] salaryCode=new String[]{
                "1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118","119","120","121","122","123","124","125","126","127","128","129","130","131","132","133","134","135","136","137","138","139","140","141","142","143","144","145","146","147","148","149","150"
        };
        List<String> salaryCodeList=new ArrayList<>();
        salaryCodeList.addAll(CollectionUtils.arrayToList(salaryCode));
        List<String> salaryCodes=dao.selectAllSalaryCode(province);
        for(String key:salaryCodes){
            String keysub=key.substring(1);
            salaryCodeList.remove(keysub);
        }
        int i=0;
        for(SalaryBasedata salaryBasedata : salaryBasedataList){//todo
            if(StringUtils.isEmpty(salaryBasedata.getId())){
                salaryBasedata.setSalaryType("0");
                salaryBasedata.setSalaryCode("s"+salaryCodeList.get(i));
                salaryBasedata.setMofDivCode(province);
                //ID为空即新增
                dao.saveSalaryBasedata(salaryBasedata);
            }else{
                String closeState = "0";//停用状态
                if(closeState.equals(salaryBasedata.getBaseState())){
                    String column = salaryBasedata.getSalaryCode();
                    for(SalaryMonthEndVO salaryMonthEndVO : salaryMonthEndVOList){
                            Method setMethod = salaryMonthEndVOClass.getDeclaredMethod("set" + column.substring(0,1).toUpperCase() + column.substring(1), Double.class);
                            setMethod.invoke(salaryMonthEndVO, Double.parseDouble("0"));
                    }
                    salaryMonthEndVOList = formulaManageService.calculateByFormula(province, agencyIds, salaryMonthEndVOList);
                    salaryMonthEndDao.batchUpdateSalaryMonthEnd(salaryMonthEndVOList);
                }
                //ID存在即编辑
                dao.updateSalaryBasedata(salaryBasedata);
            }
            i++;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSalaryBasedata(String ids) {
            dao.deleteSalaryBasedata(ids);
    }

    @Override
    public List<SalaryBasedata> querySalaryBaseDataInfo(Map<String, Object> queryParam) {
        return dao.querySalaryBaseDataInfo(queryParam);
    }

    @Override
    public List<SalaryBasedata> queryStandardBasedataInfo(Map<String, Object> queryParam) {
        return dao.queryStandardBasedataInfo(queryParam);
    }


}
